package com.soos.utils;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * Description: 做统一处理 : 登陆校验拦截器
 * 由于这里会出现一个问题,因为这只是拦截要登陆的路径,只有登录的时候才会触发
 * 这个时候 如果用户访问 非触发登陆的页面,就可能导致一直没有刷新token有效期,用户需要重新登陆
 * 所以需要再加一层拦截器,外层拦截器专门用于刷新token有效期,内层拦截器用于做登陆校验
 * User: sfx
 * Date: 2023-06-22
 * Time: 21:46
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(UserHolder.getUser() == null) {
            response.setStatus(401);
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserHolder.removeUser();
    }
}
